﻿using System;
using System.Collections.Generic;
using System.IO;
using LoungeRepo.Contracts.Core;
using LoungeRepo.Core;
using NUnit.Framework;

namespace Test.LoungeRepo.Core
{
    [TestFixture]
    public partial class testLoungeRepository
    {
        class SimpleEntity : ILoungeRepoEntityIdentity
        {
            public string Id { get; set; }
            public string Partition { get; set; }

            public SimpleEntity Child1 { get; set; }
            public SimpleEntity Child2 { get; set; }
        }


        [Test]
        public void Store_string_as_entity()
        {
            const string ID = "mystring";

            string objectFilename = string.Format(@"{0}\{1}\{2}.entity",
                                            LoungeRepository.DEFAULT_LOUNGE_REPOSITORY_ROOT_FOLDER_NAME,
                                            LoungeRepository.DEFAULT_PARTITION_NAME,
                                            ID);
            try { File.Delete(objectFilename); } catch{}

            using (var sut = new LoungeRepository())
            {
                sut.Store("hello, world!", ID);

                Assert.IsTrue(File.Exists(objectFilename));
                Assert.IsTrue(new FileInfo(objectFilename).Length > 0);
            }
        }


        [Test]
        public void Load_string_as_entity()
        {
            const string ID = "mystring";
            const string TEXT = "hello, world!";

            using (var sut = new LoungeRepository())
            {
                sut.Store(TEXT, ID);
                Assert.AreEqual(TEXT, sut.Load<string>(ID));
            }
        }


        [Test]
        public void Roundtrip_an_entity_hierarchy()
        {
            try { Directory.Delete(LoungeRepository.DEFAULT_LOUNGE_REPOSITORY_ROOT_FOLDER_NAME, true); } catch { }

            var eChild = new SimpleEntity { Id = "2", Partition = "c" };
            var eParent = new SimpleEntity { Id = "1", Partition = "p", Child1=eChild };

            using (var sut = new LoungeRepository())
            {
                sut.Store(eParent);

                var eParent2 = sut.Load<SimpleEntity>("1", "p");
                Assert.AreEqual(eParent.Id, eParent2.Id);
                Assert.AreEqual(eChild.Id, eParent2.Child1.Id);
            }
        }


        [Test]
        public void Roundtrip_an_entity_dict_and_delete_it()
        {
            var dict = new Dictionary<string, SimpleEntity>();
            dict.Add("1", new SimpleEntity { Id = "1", Partition = "simple" });
            dict.Add("2", new SimpleEntity { Id = "2", Partition = "simple" });

            using(var sut = new LoungeRepository())
            {
                sut.Store(dict, "dict");

                var e = sut.Load<SimpleEntity>("1", "simple");
                Assert.AreEqual("1", e.Id);

                var dict2 = sut.Load<Dictionary<string, SimpleEntity>>("dict");
                e = dict2["2"];
                Assert.AreEqual("2", e.Id);

                sut.Delete("dict");
                try
                {
                    sut.Load<Dictionary<string, SimpleEntity>>("dict");
                    Assert.Fail();
                }
                catch(FileNotFoundException){}
            }
        }
    }
}
